const express=require('express');
let app=express();
let cors=require('cors');//引入跨域
// 解析json
app.use(express.json());
app.use(express.urlencoded({extended:true}));
app.use(cors())//解决跨域
require('./db/db');

//2 引入ejs
const ejs=require('ejs');
// 设置ejs模板
app.set('view engine','ejs');
// 设置静态资源
app.use(express.static('public'))

// 引入数据库
const mongs=require('mongoose');



let session=require('express-session')

app.use(session({
    secret:'feitainduidi',
    resave:false,
    saveUninitialized:true,
    cookie:{
        maxAge:24*60*60*1000
    },
    rolling:true
}))


// let localStorage=require('localStorage')
app.use((req,res,next)=>{
    // req.headers.token=localStorage.getItem('toket')||'';
    // 
    if(!req.session.username&& req.url!='/index/login' && req.url!='/index/dologin'){
        res.redirect('/index/login')
    }else{
        next()
    }


})
// 解析json
app.use(express.urlencoded({extended:false}))
app.use(express.json())


// 写入一级路由

app.use('/index',require('./route/routelist'))

app.listen(3000,()=>{
    console.log('3000 isrunding');
})